//
//  SettingViewController.swift
//  whack-a-mole
//
//  Created by wxc on 2019/9/19.
//  Copyright © 2019 wxc. All rights reserved.
//

import UIKit

class SettingViewController: UIViewController {

    private lazy var speed_lb : UILabel = {
        let speed_lb = UILabel.init()
        speed_lb.frame = CGRect.init(x: 50, y: 50, width: 100, height: 30)
        speed_lb.font = UIFont.systemFont(ofSize: UIFont.labelFontSize)
        speed_lb.textColor = UIColor.black
        speed_lb.text = "Speed : "
        return speed_lb
    }()
    
    var isplayer : Bool = true
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }

}

extension SettingViewController {
    @objc func backClick() -> Void {
        self.dismiss(animated: true, completion: nil)
    }
    
    @objc func sliderValueChange(slider : UISlider) -> Void {
        let value = slider.value
        UserDefaults.standard.set(value, forKey: "speed")
        UserDefaults.standard.synchronize()
    }
    
    @objc func soundSwitchChanged(sender : UISwitch) -> Void {
        if sender.isOn
        {
            NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: NotifictionKey_Sound_Play), object: nil)
            UserDefaults.standard.set("true", forKey: SoundIsPlayKey)
            UserDefaults.standard.synchronize()
        }
        else
        {
            NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: NotifictionKey_Sound_Pause), object: nil)
            UserDefaults.standard.set("false", forKey: SoundIsPlayKey)
            UserDefaults.standard.synchronize()
        }
    }
    
    @objc func timeSwitchChanged(sender : UISwitch) -> Void {
        if sender.isOn
        {
            UserDefaults.standard.set("on", forKey: ShowTimerKey)
            UserDefaults.standard.synchronize()
        }
        else
        {
            UserDefaults.standard.set("off", forKey: ShowTimerKey)
            UserDefaults.standard.synchronize()
        }
    }
    
    @objc func timeGoalSwitchChanged(sender : UISwitch) -> Void {
        if sender.isOn
        {
            UserDefaults.standard.set("on", forKey: ShowTimerGoalKey)
            UserDefaults.standard.synchronize()
        }
        else
        {
            UserDefaults.standard.set("off", forKey: ShowTimerGoalKey)
            UserDefaults.standard.synchronize()
        }
    }
    
    @objc func repsSwitchChanged(sender : UISwitch) -> Void {
        if sender.isOn
        {
            UserDefaults.standard.set("on", forKey: ShowRepsKey)
            UserDefaults.standard.synchronize()
        }
        else
        {
            UserDefaults.standard.set("off", forKey: ShowRepsKey)
            UserDefaults.standard.synchronize()
        }
    }
    
    @objc func repsGoalSwitchChanged(sender : UISwitch) -> Void {
        if sender.isOn
        {
            UserDefaults.standard.set("on", forKey: ShowRepsGoalKey)
            UserDefaults.standard.synchronize()
        }
        else
        {
            UserDefaults.standard.set("off", forKey: ShowRepsGoalKey)
            UserDefaults.standard.synchronize()
        }
    }
    
    @objc func repsSemgentChanged(sender : UISegmentedControl) -> Void {
        if sender.selectedSegmentIndex == 0
        {
            UserDefaults.standard.set("up", forKey: ShowRepsUpAndDownKey)
            UserDefaults.standard.synchronize()
        }
        else
        {
            UserDefaults.standard.set("down", forKey: ShowRepsUpAndDownKey)
            UserDefaults.standard.synchronize()
        }
    }
    
    @objc func timeSemgentChanged(sender : UISegmentedControl) -> Void {
        if sender.selectedSegmentIndex == 0
        {
            UserDefaults.standard.set("up", forKey: ShowTimerUpAndDownKey)
            UserDefaults.standard.synchronize()
        }
        else
        {
            UserDefaults.standard.set("down", forKey: ShowTimerUpAndDownKey)
            UserDefaults.standard.synchronize()
        }
    }
    
    
    func initUI() -> Void {
        view.backgroundColor = UIColor.white
        let speed : Double = UserDefaults.standard.object(forKey: "speed") as! Double

        view.addSubview(self.speed_lb)
        
        let slider = UISlider(frame: CGRect.init(x: speed_lb.zc_right + 10, y: speed_lb.zc_centerY, width: ScreenWidth - 220, height: 20))
        slider.maximumValue = 10
        slider.minimumValue = 1
        slider.setValue(Float(speed), animated: true)
        slider.isContinuous = false
        slider.addTarget(self, action: #selector(sliderValueChange(slider:)), for: .valueChanged)
        view.addSubview(slider)
        
        // goal reps
        let GoalReps_lb = UILabel.init()
        GoalReps_lb.frame = CGRect.init(x: speed_lb.zc_x, y: speed_lb.zc_bottom + 20, width: 400, height: 30)
        GoalReps_lb.font = UIFont.systemFont(ofSize: UIFont.labelFontSize+10)
        GoalReps_lb.textColor = UIColor.black
        GoalReps_lb.text = "Goal Reps Settings:"
        view.addSubview(GoalReps_lb)
        
        let show_reps_lb = UILabel.init()
        show_reps_lb.frame = CGRect.init(x: GoalReps_lb.zc_x, y: GoalReps_lb.zc_bottom + 10, width: 100, height: 30)
        show_reps_lb.font = UIFont.systemFont(ofSize: UIFont.labelFontSize-2)
        show_reps_lb.textColor = UIColor.black
        show_reps_lb.text = "Show Reps: "
        view.addSubview(show_reps_lb)
        
        let show_reps_switch = UISwitch.init()
        show_reps_switch.frame = CGRect.init(x: show_reps_lb.zc_right + 10, y: show_reps_lb.zc_y, width: 100, height: 30)
        show_reps_switch.addTarget(self, action: #selector(repsSwitchChanged(sender:)), for: .valueChanged)
        show_reps_switch.setOn(true, animated: true)
        view.addSubview(show_reps_switch)
        if UserDefaults.standard.object(forKey: ShowRepsKey) != nil
        {
            if "\(UserDefaults.standard.object(forKey: ShowRepsKey) ?? "on")" == "on"
            {
                show_reps_switch.setOn(true, animated: true)
            }
            else
            {
                show_reps_switch.setOn(false, animated: true)
            }
        }
        
        let show_reps_Goal_lb = UILabel.init()
        show_reps_Goal_lb.frame = CGRect.init(x: show_reps_switch.zc_right + 10, y: show_reps_lb.zc_y, width: 150, height: 30)
        show_reps_Goal_lb.font = UIFont.systemFont(ofSize: UIFont.labelFontSize-2)
        show_reps_Goal_lb.textColor = UIColor.black
        show_reps_Goal_lb.text = "Show Reps Goal: "
        view.addSubview(show_reps_Goal_lb)
        
        let show_reps_goal_switch = UISwitch.init()
        show_reps_goal_switch.frame = CGRect.init(x: show_reps_Goal_lb.zc_right + 10, y: show_reps_lb.zc_y, width: 100, height: 30)
        show_reps_goal_switch.addTarget(self, action: #selector(repsGoalSwitchChanged(sender:)), for: .valueChanged)
        show_reps_goal_switch.setOn(true, animated: true)
        view.addSubview(show_reps_goal_switch)
        if UserDefaults.standard.object(forKey: ShowRepsGoalKey) != nil
        {
            if "\(UserDefaults.standard.object(forKey: ShowRepsGoalKey) ?? "on")" == "on"
            {
                show_reps_goal_switch.setOn(true, animated: true)
            }
            else
            {
                show_reps_goal_switch.setOn(false, animated: true)
            }
        }
        
        let reps_Segment = UISegmentedControl.init()
        reps_Segment.insertSegment(withTitle: "Up", at: 0, animated: true)
        reps_Segment.insertSegment(withTitle: "Down", at: 1, animated: true)
        reps_Segment.frame = CGRect.init(x: show_reps_goal_switch.zc_right + 30, y: show_reps_lb.zc_y, width: 100, height: 30)
        reps_Segment.addTarget(self, action: #selector(repsSemgentChanged(sender:)), for: .valueChanged)
        view.addSubview(reps_Segment)
        
        if UserDefaults.standard.object(forKey: ShowRepsUpAndDownKey) != nil
        {
            if "\(UserDefaults.standard.object(forKey: ShowRepsUpAndDownKey) ?? "up")" == "up"
            {
                reps_Segment.selectedSegmentIndex = 0
            }
            else
            {
                reps_Segment.selectedSegmentIndex = 1
            }
        }
        else
        {
            reps_Segment.selectedSegmentIndex = 0
        }
        
        // timer
        let Timer_lb = UILabel.init()
        Timer_lb.frame = CGRect.init(x: show_reps_lb.zc_x, y: show_reps_lb.zc_bottom + 20, width: 400, height: 30)
        Timer_lb.font = UIFont.systemFont(ofSize: UIFont.labelFontSize+10)
        Timer_lb.textColor = UIColor.black
        Timer_lb.text = "Timer Settings:"
        view.addSubview(Timer_lb)
        
        let show_time_lb = UILabel.init()
        show_time_lb.frame = CGRect.init(x: Timer_lb.zc_x, y: Timer_lb.zc_bottom + 10, width: 100, height: 30)
        show_time_lb.font = UIFont.systemFont(ofSize: UIFont.labelFontSize-2)
        show_time_lb.textColor = UIColor.black
        show_time_lb.text = "Show Timer: "
        view.addSubview(show_time_lb)
        
        let show_time_switch = UISwitch.init()
        show_time_switch.frame = CGRect.init(x: show_time_lb.zc_right + 10, y: show_time_lb.zc_y, width: 100, height: 30)
        show_time_switch.addTarget(self, action: #selector(timeSwitchChanged(sender:)), for: .valueChanged)
        show_time_switch.setOn(true, animated: true)
        view.addSubview(show_time_switch)
        if UserDefaults.standard.object(forKey: ShowTimerKey) != nil
        {
            if "\(UserDefaults.standard.object(forKey: ShowTimerKey) ?? "on")" == "on"
            {
                show_time_switch.setOn(true, animated: true)
            }
            else
            {
                show_time_switch.setOn(false, animated: true)
            }
        }
        
        let show_time_Goal_lb = UILabel.init()
        show_time_Goal_lb.frame = CGRect.init(x: show_time_switch.zc_right + 10, y: Timer_lb.zc_bottom + 10, width: 150, height: 30)
        show_time_Goal_lb.font = UIFont.systemFont(ofSize: UIFont.labelFontSize-2)
        show_time_Goal_lb.textColor = UIColor.black
        show_time_Goal_lb.text = "Show Timer Goal: "
        view.addSubview(show_time_Goal_lb)
        
        let show_time_goal_switch = UISwitch.init()
        show_time_goal_switch.frame = CGRect.init(x: show_time_Goal_lb.zc_right + 10, y: show_time_lb.zc_y, width: 100, height: 30)
        show_time_goal_switch.addTarget(self, action: #selector(timeGoalSwitchChanged(sender:)), for: .valueChanged)
        view.addSubview(show_time_goal_switch)
        show_time_goal_switch.setOn(true, animated: true)
        if UserDefaults.standard.object(forKey: ShowTimerGoalKey) != nil
        {
            if "\(UserDefaults.standard.object(forKey: ShowTimerGoalKey) ?? "on")" == "on"
            {
                show_time_goal_switch.setOn(true, animated: true)
            }
            else
            {
                show_time_goal_switch.setOn(false, animated: true)
            }
        }
        
        let time_Segment = UISegmentedControl.init()
        time_Segment.insertSegment(withTitle: "Up", at: 0, animated: true)
        time_Segment.insertSegment(withTitle: "Down", at: 1, animated: true)
        time_Segment.frame = CGRect.init(x: show_time_goal_switch.zc_right + 30, y: show_time_goal_switch.zc_y, width: 100, height: 30)
        time_Segment.addTarget(self, action: #selector(timeSemgentChanged(sender:)), for: .valueChanged)
        view.addSubview(time_Segment)

        if UserDefaults.standard.object(forKey: ShowTimerUpAndDownKey) != nil
        {
            if "\(UserDefaults.standard.object(forKey: ShowTimerUpAndDownKey) ?? "up")" == "up"
            {
                time_Segment.selectedSegmentIndex = 0
            }
            else
            {
                time_Segment.selectedSegmentIndex = 1
            }
        }
        else
        {
            time_Segment.selectedSegmentIndex = 0
        }

        
        // other
        let Other_lb = UILabel.init()
        Other_lb.frame = CGRect.init(x: show_time_lb.zc_x, y: show_time_lb.zc_bottom + 20, width: 400, height: 30)
        Other_lb.font = UIFont.systemFont(ofSize: UIFont.labelFontSize+10)
        Other_lb.textColor = UIColor.black
        Other_lb.text = "Other Settings:"
        view.addSubview(Other_lb)
        
        let sound_lb = UILabel.init()
        sound_lb.frame = CGRect.init(x: Other_lb.zc_x, y: Other_lb.zc_bottom + 10, width: 100, height: 30)
        sound_lb.font = UIFont.systemFont(ofSize: UIFont.labelFontSize-2)
        sound_lb.textColor = UIColor.black
        sound_lb.text = "Play Sounds: "
        view.addSubview(sound_lb)
        
        let sound_switch = UISwitch.init()
        sound_switch.frame = CGRect.init(x: sound_lb.zc_right + 10, y: sound_lb.zc_y, width: 100, height: 30)
        sound_switch.addTarget(self, action: #selector(soundSwitchChanged(sender:)), for: .valueChanged)
        view.addSubview(sound_switch)
        
        if self.isplayer
        {
            sound_switch.setOn(true, animated: true)
        }
        else
        {
            sound_switch.setOn(false, animated: true)
        }
      
        let done_btn = UIButton.init()
        done_btn.setTitle("Down", for: .normal)
        done_btn.setTitleColor(.black, for: .normal)
        done_btn.backgroundColor = UIColor.lightGray
        done_btn.titleLabel?.font = UIFont.systemFont(ofSize: UIFont.labelFontSize)
        done_btn.addTarget(self, action: #selector(backClick), for: .touchUpInside)
        done_btn.frame = CGRect.init(x: ScreenWidth/2 - 100, y: sound_lb.zc_bottom + 100, width: 200, height: 50)
        view.addSubview(done_btn)
    }
}
